<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
          <!-- 用一个行列布局 -->
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>

          <!-- 放置一个el-tree组件 -->
          <el-tree :data="list" :default-expand-all="true">
            <template #default="{data}">
               <!-- 用一个行列布局 -->
               <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%;">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span>{{data.name}}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{data.manager}}</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                      <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
            </template>
          </el-tree>
      </el-card>
      <!-- 实现新增子部门或者编辑部门的对话框 -->
      <el-dialog @close="resetForm" :title="isEdit?'编辑部门':'新增部门'" :visible.sync="showDialog" :close-on-click-modal='false' :close-on-press-escape='false'>
        <!-- 在弹层中放入子组件 -->
        <!-- <depaDialog v-if="showDialog" :id="curId" @success="hSuccess" :isEdit="isEdit" @close="showDialog=false" :originList="originList" /> -->
        <depaDialog ref="depaDialogRef" :id="curId" @success="hSuccess" :isEdit="isEdit" @close="showDialog=false" :originList="originList" />

      </el-dialog>   
    </div>
  </div>
</template>

<script>
  import {getDepartments,DelDepartmentsEdit} from '@/api/departments'
  import {tranListToTreeData} from '@/utils/index'
  import depaDialog from './depaDialog.vue'
  export default {
    components:{
      depaDialog
    },
    data() {
      return {
        showDialog:false, //控制新增子部门或者编辑部门的对话框的显示和隐藏
       // 依赖一份树形数据
        list: [],//部门列表
        curId:'',//保存当前部门的id
        isEdit:false, //false是进入新增页面 true是编辑页面
        originList:[]
      }
    },
    created(){
      this.loadDepartments()
    },
    methods: {
      // 重置子组件中的表单
      resetForm(){
        // 通过父组件中调用子组件的重置方法实现resetForm实现重置表单的功能
        this.$refs.depaDialogRef.resetForm()
      },
      hSuccess(){
        // 关闭弹框
        this.showDialog = false
        // 重新渲染数据
      this.loadDepartments()
      },
      // 编辑部分
      hEdit(id){
        this.curId = id
        this.isEdit = true
        this.showDialog = true
        this.$nextTick(()=>{
          this.$refs.depaDialogRef.loadDepartmentsEdit()
        })
      },
      // 新增部分
      hAdd(id){
        this.curId = id
        this.isEdit = false
        this.showDialog = true
      },
      // 删除部门
      hDel(id){
        this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const res = await DelDepartmentsEdit(id)
          this.$message.success(res.message)
          this.loadDepartments()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      async loadDepartments(){
      try{
        const res = await getDepartments()
        console.log(res);
        this.originList = res.data.depts.map(item=>{
          return {id:item.id,pid:item.pid,code:item.code,name:item.name}
        })
        res.data.depts.shift()
        this.list = tranListToTreeData (res.data.depts)
      }catch(error){
        console.log(error);
      }
      }
    },
  }
  </script>